package com.sg.java.util;

import com.sg.java.entity.VoltageQualified;

import java.util.ArrayList;
import java.util.HashMap;

public class FormulaUtils_bak {

    public static void IsVoltageQualified(VoltageQualified voltage) {
        Double monitorTime = voltage.getMonitorTime();
        Double upTime = voltage.getUpTime();
        Double downTime = voltage.getDownTime();
        Double totalU = voltage.getTotalU();
        Double notNullPoint = voltage.getNotNullPoint();
        Double effectiveDataTime = voltage.getEffectiveDataTime();
        Integer dyCount = voltage.getDyCount();
        Integer bl = 1;
        if (dyCount.equals(96)) {
            bl = 1;
        } else if (dyCount.equals(24)) {
            bl = 4;
        }


        Double u = voltage.getVoltage();

        //填充用户配变长期 严重集合
        yhLongTimeUper(voltage);
        yhLongTimeDown(voltage);
        yhYzDown(voltage);
        yhYzUp(voltage);

        voltage.setNotNullPoint(notNullPoint + 1);
        if (u > voltage.getUp()) { //230
            upTime += 15 * bl;
            //越上限电压值时刻
            voltage.getUpBuilder().append(voltage.getIndex().toString()).append(",");
            voltage.setUpTime(upTime);

        }
        if (u < voltage.getDown()) { //190
            downTime += 15 * bl;
            //越下限电压值时刻
            voltage.getDownBuilder().append(voltage.getIndex().toString()).append(",");
            voltage.setDownTime(downTime);
        }

//        8000000051402003

        monitorTime += 15 * bl;

        voltage.setMonitorTime(monitorTime);

        voltage.setTotalU(totalU + u);

        Double effectiveDataUper = voltage.getEffectiveDataUper();
        Double effectiveDataLower = voltage.getEffectiveDataLower();

        //  判断电压有效
        if (u > effectiveDataLower && u < effectiveDataUper) {
            effectiveDataTime += 15 * bl;
            voltage.setEffectiveDataTime(effectiveDataTime);

        }

        int offset = voltage.getOffset();
        Double maxU = voltage.getMax();
        Double minU = voltage.getMin();

        // 处理最大和最小电压
        if (offset == 1) {
            voltage.setMax(u);
            voltage.setMin(u);
        }

        if (u > maxU) {
            voltage.setMax(u);
            voltage.setMaxUOffset(offset);
        }

        if (u < minU) {
            voltage.setMin(u);
            voltage.setMinUOffset(offset);
        }

        voltage.setOffset(offset + 1);
    }

    //填充用户配变长期越上集合
    public static void yhLongTimeUper(VoltageQualified voltageQualified) {

        //获取当前电压值
        Double voltage = voltageQualified.getVoltage();
        ArrayList<Integer> yhLongTimeUperArray = voltageQualified.getYhLongTimeUperArray();
        Integer index = voltageQualified.getIndex();
        if (voltage > voltageQualified.getUp()) {

            yhLongTimeUperArray.add(index);

        }
    }

    //填充用户配变长期越下集合
    public static void yhLongTimeDown(VoltageQualified voltageQualified) {
        Double voltage = voltageQualified.getVoltage();
        ArrayList<Integer> yhLongTimeDownArray = voltageQualified.getYhLongTimeDownArray();
        Integer index = voltageQualified.getIndex();
        if (voltage < voltageQualified.getDown()) {
            yhLongTimeDownArray.add(index);
        }
    }

    //填充用户配变严重越上集合
    public static void yhYzUp(VoltageQualified voltageQualified) {
        Double voltage = voltageQualified.getVoltage();
        ArrayList<Integer> yhSeverityUperArray = voltageQualified.getYhSeverityUperArray();
        Integer index = voltageQualified.getIndex();
        if (voltage > voltageQualified.getYzYhPbUper()) {
            yhSeverityUperArray.add(index);
        }
    }

    //填充用户配变严重越下集合
    public static void yhYzDown(VoltageQualified voltageQualified) {
        Double voltage = voltageQualified.getVoltage();
        ArrayList<Integer> yhSeverityDownArray = voltageQualified.getYhSeverityDownArray();
        Integer index = voltageQualified.getIndex();
        if (voltage < voltageQualified.getYzYhPbDown()) {
            yhSeverityDownArray.add(index);
        }
    }

    //判断用户类型 长期高低电压 严重高低电压
    public static void consType(VoltageQualified voltageQualified) {

        //1长期高 2长期低 3严重高  4严重低
        StringBuffer consTypeBuffer = new StringBuffer();
        ArrayList<Integer> yhLongTimeUperArray = voltageQualified.getYhLongTimeUperArray();
        ArrayList<Integer> yhLongTimeDownArray = voltageQualified.getYhLongTimeDownArray();
        ArrayList<Integer> yhSeverityUperArray = voltageQualified.getYhSeverityUperArray();
        ArrayList<Integer> yhSeverityDownArray = voltageQualified.getYhSeverityDownArray();
        //判断是按96个点来算 还是24个点来算
        Integer dyCount = voltageQualified.getDyCount();
        if (voltageQualified.getDyCount() == 24) {
            if (yhLongTimeUperArray.size() > 0) {
                consTypeBuffer.append("1,");
            }
            if (yhLongTimeDownArray.size() > 0) {
                consTypeBuffer.append("2,");
            }
            if (yhSeverityUperArray.size() > 0) {
                consTypeBuffer.append("3,");
            }
            if (yhSeverityDownArray.size() > 0) {
                consTypeBuffer.append("4");
            }
        } else if (voltageQualified.getDyCount() == 96) {

            boolean longTimeUper = isLxNum(yhLongTimeUperArray);
            if (longTimeUper) {
                consTypeBuffer.append("1,");
            }

            boolean longTimeDown = isLxNum(yhLongTimeDownArray);
            if (longTimeDown) {
                consTypeBuffer.append("2,");
            }

            boolean severityUper = isLxNum(yhSeverityUperArray);
            if (severityUper) {
                consTypeBuffer.append("3,");
            }

            boolean severityDown = isLxNum(yhSeverityDownArray);
            if (severityDown) {
                consTypeBuffer.append("4");
            }
        }
        voltageQualified.setConsType(consTypeBuffer.toString());
    }


    //判断一个集合是否有四个连续的数字
    public static boolean isLxNum(ArrayList<Integer> integers) {

        if (integers.size() < 4) {
            return false;
        }
//        //长度
//        for (int i = 0; i <integers.size(); i++) {
//            if(i+3 <= integers.size()){
//                Integer tempNum = integers.get(i);
//                if (integers.get(i+1) == tempNum+1 && integers.get(i+2) == tempNum+2 && integers.get(i+3) == tempNum+3){
//                    return true;
//                }
//            }
//
//        }
//        return false;

        for (int i = 0; i < integers.size(); i++) {
            for (int j = 1; j < integers.size(); j++) {
                for (int k = 2; k < integers.size(); k++) {
                    for (int l = 3; l < integers.size(); l++) {
                        if (integers.get(l) - integers.get(k) == 1 &&
                                integers.get(k) - integers.get(j) == 1 &&
                                integers.get(j) - integers.get(i) == 1) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }




    //判断一个集合是否有四个连续的数字
    public static Integer isLxNum2(ArrayList<Integer> integers) {

        int q = 0;

        if (integers.size() < 4) {
            return 0;
        }

        for (int i = 0; i < integers.size(); i++) {
            for (int j = 1; j < integers.size(); j++) {
                for (int k = 2; k < integers.size(); k++) {
                    for (int l = 3; l < integers.size(); l++) {
                        if (integers.get(l) - integers.get(k) == 1 &&
                                integers.get(k) - integers.get(j) == 1 &&
                                integers.get(j) - integers.get(i) == 1) {
                            q++;
                        }
                    }
                }
            }
        }
        return q;
    }



    //配变越上越下次数
    public static void Pbcs(VoltageQualified voltageQualified){
        ArrayList<Integer> yhLongTimeUperArray = voltageQualified.getYhLongTimeUperArray();
        ArrayList<Integer> yhLongTimeDownArray = voltageQualified.getYhLongTimeDownArray();
        ArrayList<Integer> yhSeverityUperArray = voltageQualified.getYhSeverityUperArray();
        ArrayList<Integer> yhSeverityDownArray = voltageQualified.getYhSeverityDownArray();

        if (voltageQualified.getDyCount() == 24){
            if (yhSeverityUperArray.size()>0){
                voltageQualified.setPbType("1");
                voltageQualified.setPbcs(yhSeverityUperArray.size());
            }else if (yhLongTimeUperArray.size()>0){
                voltageQualified.setPbType("2");
                voltageQualified.setPbcs(yhLongTimeUperArray.size());
            }else if (yhSeverityDownArray.size()>0){
                voltageQualified.setPbType("3");
                voltageQualified.setPbcs(yhSeverityDownArray.size());
            }else if (yhLongTimeDownArray.size()>0){
                voltageQualified.setPbType("4");
                voltageQualified.setPbcs(yhLongTimeDownArray.size());
            }
        }else if (voltageQualified.getDyCount() == 96){
            boolean longTimeUper = isLxNum(yhLongTimeUperArray);
            boolean longTimeDown = isLxNum(yhLongTimeDownArray);
            boolean severityUper = isLxNum(yhSeverityUperArray);
            boolean severityDown = isLxNum(yhSeverityDownArray);
            if (severityUper){
                voltageQualified.setPbType("1");
                Integer lxNum2 = isLxNum2(yhSeverityUperArray);
                voltageQualified.setPbcs(lxNum2);
            }else if (longTimeUper){
                voltageQualified.setPbType("2");
                Integer lxNum2 = isLxNum2(yhLongTimeUperArray);
                voltageQualified.setPbcs(lxNum2);
            }else if (severityDown){
                voltageQualified.setPbType("3");
                Integer lxNum2 = isLxNum2(yhSeverityDownArray);
                voltageQualified.setPbcs(lxNum2);
            }else if (longTimeDown){
                voltageQualified.setPbType("4");
                Integer lxNum2 = isLxNum2(yhLongTimeDownArray);
                voltageQualified.setPbcs(lxNum2);
            }

        }

    }


    //配变功率因数计算
    public static Double powerFactor(Double yg, Double ug) {
        return yg / (Math.sqrt(((yg * yg) + (ug * ug))));
    }

    //配变视在功率计算 : 有功的平方 + 无功的平方 开方
    public static Double apparentPower(Double yg, Double wg) {
        if (yg != null && wg != null) {
            return (Math.sqrt(((yg * yg) + (wg * wg))));
        } else {
            return 0D;
        }
    }

    //配变负载率计算
    public static Double loadRate(Double apparentPower, Double rated) {
        if (rated != 0) {
            return apparentPower / rated;
        } else {
            return 0D;
        }
    }


    public static void pbYzYs(VoltageQualified voltageQualified) {

        Integer yzyxTime = voltageQualified.getYzyxTime();
        Double u = voltageQualified.getVoltage();
        if (u > voltageQualified.getYzYhPbUper()) {
            yzyxTime += 15;
            voltageQualified.setYzyxTime(yzyxTime);
        }

    }


    //三相不平衡计算公式:（最大电流-最小电流）/最大电流
    public static Double triphase(Double max, Double min) {
        return (max - min) / max;
    }

    //计算电压波动率计算公式: （最高电压-额定电压/额定电压）×100%
    public static Double voltageFluctuation(Double maxVoltage, String dydj) {
        //根据电压等级 获取额定电压值
//        Double desc = Constant.VoltEnum.getDesc(dydj);
        //暂时写死380v
        Double desc = 380D;
        if (desc != null && desc != 0) {
            return (maxVoltage - desc) / desc;
        } else {
            return 0D;
        }
    }


}
